### Preparatory stuff (could be skipped) #### 

library(readr)
library(readxl)
library(openxlsx)
library(igraph)
library(igraphdata)
library(sqldf)

data01 <-read_excel('data.xlsx') 
data <- data01[c(1,2)]
write_csv(data,'edges21.csv')

prenodes22 <- read.csv('nodes22.csv')
prenodes22 <- sqldf('select * from prenodes22 order by Id')
write.xlsx(prenodes22,'prenodes22_1.xlsx')

prenodes <- read.csv('nodes_stat21.csv')
write.xlsx(prenodes,'prenodes.xlsx')
write_csv(nodes,'nodes21.csv')

prenodes20 <- read_csv('nodes20.csv')
prenodes20 <- sqldf("select * from prenodes20 order by Id")
write.xlsx(prenodes20,"prenodes20_1.xlsx")

prenodes19 <- read_csv('nodes19.csv')
prenodes19 <- sqldf("select * from prenodes19 order by Id")
write.xlsx(prenodes19,'prenodes19_1.xlsx')

### Networks ###

nodes22 <- read_xlsx('prenodes22_1.xlsx')
edges22 <- read_xlsx('data22.xlsx')
nodes22$country <- as.factor(nodes22$country)
nodes22$type <- as.factor(nodes22$type)
nodes22$arcticness <- as.factor(nodes22$arcticness)
net22 <- graph_from_data_frame(d=edges22, vertices=nodes22, directed = F)
net22 <- simplify(net22, remove.loops = T)

nodes <- read_xlsx('prenodes.xlsx')
edges <- data
nodes$country <- as.factor(nodes$country)
nodes$type <- as.factor(nodes$type)
nodes$arcticness <- as.factor(nodes$arcticness)
net <- graph_from_data_frame(d=edges, vertices=nodes, directed = F)
net <- simplify(net, remove.loops = T)

nodes20 <- read_xlsx('prenodes20_1.xlsx')
edges20 <- read_xlsx('data20.xlsx')
nodes20$country <- as.factor(nodes20$country)
nodes20$type <- as.factor(nodes20$type)
nodes20$arcticness <- as.factor(nodes20$arcticness)
net20 <- graph_from_data_frame(d=edges20, vertices=nodes20, directed = F)
net20 <- simplify(net20, remove.loops = T)

nodes19 <- read_xlsx('prenodes19_1.xlsx')
edges19 <- read_xlsx('data19.xlsx')
nodes19$country <- as.factor(nodes19$country)
nodes19$type <- as.factor(nodes19$type)
nodes19$arcticness <- as.factor(nodes19$arcticness)
net19 <- graph_from_data_frame(d=edges19, vertices=nodes19, directed = F)
net19 <- simplify(net19, remove.loops = T)

### Shaping and plotting ###

V(net22)$label.color="black"
V(net22)$vertex.label=nodes22$Id
V(net22)$shape='square'
V(net22)$shape = ifelse(V(net22)$arcticness == 'yes', 'sphere', V(net22)$shape)
V(net22)$shape = ifelse(V(net22)$arcticness == 'no', 'circle', V(net22)$shape)
V(net22)$color = 'cadetblue1'
V(net22)$size=7
set.seed(802)
plot(net22)

V(net)$label.color="black"
V(net)$vertex.label=nodes$Id
V(net)$shape='square'
V(net)$shape = ifelse(V(net)$arcticness == 'yes', 'sphere', V(net)$shape)
V(net)$shape = ifelse(V(net)$arcticness == 'no', 'circle', V(net)$shape)
V(net)$color = 'cadetblue1'
V(net)$size=7
set.seed(802)
plot(net)

V(net20)$label.color="black"
V(net20)$vertex.label=nodes20$Id
V(net20)$shape='square'
V(net20)$shape = ifelse(V(net20)$arcticness == 'yes', 'sphere', V(net20)$shape)
V(net20)$shape = ifelse(V(net20)$arcticness == 'no', 'circle', V(net20)$shape)
V(net20)$color = 'cadetblue1'
V(net20)$size=7
set.seed(802)
plot(net20)

V(net19)$label.color="black"
V(net19)$vertex.label=nodes19$Id
V(net19)$shape='square'
V(net19)$shape = ifelse(V(net19)$arcticness == 'yes', 'sphere', V(net19)$shape)
V(net19)$shape = ifelse(V(net19)$arcticness == 'no', 'circle', V(net19)$shape)
V(net19)$color = 'cadetblue1'
V(net19)$size=7
set.seed(802)
plot(net19)

### Visualizing and plotting ###

V(net)$label.color="black"
V(net)$vertex.label=nodes$Id
V(net)$shape='circle'
coul <- c("deepskyblue1","gold1",'chartreuse2','coral2','darkseagreen3','firebrick3')
my_color <- coul[as.numeric(as.factor(V(net)$type))]
V(net)$color= my_color
V(net)$size=7
set.seed(802)
plot(net)

V(net20)$label.color="black"
V(net20)$vertex.label=nodes20$Id
V(net20)$shape='circle'
coul20 <- c("deepskyblue1","gold1",'chartreuse2','coral2','darkseagreen3','firebrick3')
my_color20 <- coul20[as.numeric(as.factor(V(net20)$type))]
V(net20)$color= my_color20
V(net20)$size=7
set.seed(802)
plot(net20)

V(net19)$label.color="black"
V(net19)$vertex.label=nodes19$Id
V(net19)$shape='circle'
coul19 <- c("deepskyblue1","gold1",'chartreuse2','coral2','darkseagreen3','firebrick3')
my_color19 <- coul19[as.numeric(as.factor(V(net19)$type))]
V(net19)$color= my_color19
V(net19)$size=7
set.seed(802)
plot(net19)


### Descriptive statistics ####

summary(net19, print.adj=T)
summary(net20, print.adj=T)
summary(net, print.adj=T)
summary(net22, print.adj=T)

graph.density(net19)
graph.density(net20)
graph.density(net)
graph.density(net22)

diameter(net19,directed=F, weights=NA)
diameter(net20,directed=F, weights=NA)
diameter(net,directed=F, weights=NA)
diameter(net22,directed=F, weights=NA)

centr_betw(net19, directed=F, normalized=T)
centr_betw(net20, directed=F, normalized=T)
centr_betw(net, directed=F, normalized=T)
centr_betw(net22, directed=F, normalized=T)

components(net19)
components(net20)
components(net)
components(net22)

